package com.vimi8.ebb.util;


import com.vimi8.ebb.model.GeoCodings;
import org.apache.commons.lang.StringUtils;

/**
 * Created by think on 2016/8/1.
 */
public class GeoCodingUtil {
    /**
     * 将区划id转换为GeoCodings的model
     *
     * @param aid 区划id
     * @return GeoCodings
     */
    public static GeoCodings convertById(String aid) {
        long areaid = Long.parseLong(aid);
        int prov = (int) (areaid / Math.pow(10, 10));
        int city = (int) (areaid / Math.pow(10, 8) % Math.pow(10, 2));
        int county = (int) (areaid / Math.pow(10, 6) % Math.pow(10, 2));
        int village = (int) (areaid / Math.pow(10, 3) % Math.pow(10, 3));
        int burg = (int) (areaid % Math.pow(10, 3));

        GeoCodings result = new GeoCodings();
        result.setId(aid);
        result.setProv(prov);
        if (city > 0) {
            result.setCity(city);
            result.setRegionLever(2);
        }
        if (county > 0) {
            result.setCounty(county);
            result.setRegionLever(3);
        }
        if (village > 0) {
            result.setVillage(village);
            result.setRegionLever(4);
        }
        if (burg > 0) {
            result.setBurg(burg);
            result.setRegionLever(5);
        }

        return result;
    }

    /**
     * 将AddressCode转换为含通配符的字符串
     *
     * @param addressCode
     * @param level
     * @return
     */
    public static String separateAddressCode(String addressCode, int level) {
        if (StringUtils.isEmpty(addressCode)) {
            return null;
        }
        return level == 1 ? addressCode.substring(0, 2) + "%" ://省级
                level == 2 ? addressCode.substring(0, 4) + "%" ://市级
                        level == 3 ? addressCode.substring(0, 6) + "%" ://县级
                                level == 4 ? addressCode.substring(0, 9) + "%" : //乡级
                                        addressCode;//村级
    }
}
